//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package rtk.olinkstar;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

public enum LocationDataManager {
    INSTANSE;

    public boolean bdsExist = false;
    public boolean gpsExist = false;
    public boolean glsExist = false;
    public static final int MAX_LOCATIONS_SIZE = 7200;
    public static final int MAX_RSSI_SIZE = 7200;
    private MyLocation currentLocation = new MyLocation();
    private int currentRssi = 0;
    private CopyOnWriteArrayList<MySatellite> satellites = new CopyOnWriteArrayList();
    private String strGGA = "";
    private CopyOnWriteArrayList<MyLocation> locationCache = new CopyOnWriteArrayList();
    private ArrayList rssisCache = new ArrayList();
    private boolean located = false;
    private boolean satelliteFound = false;
    private double maxLat = 0.0D;
    private double minLat = 0.0D;
    private double maxLon = 0.0D;
    private double minLon = 0.0D;
    private double maxAlt = 0.0D;
    private double minAlt = 0.0D;
    private double maxBaseDelay = 0.0D;
    private double minBaseDelay = 0.0D;
    private double maxBaseLine = 0.0D;
    private double minBaseLine = 0.0D;
    protected static final String TAG = "TAGLocationDataManager";

    private LocationDataManager() {
    }

    public double getAvgSNR() {
        boolean a = false;
        double b = this.getAvgSnr();
        int a1 = (int)Math.round(b);
        return (double)a1;
    }

    public double getAvgELV() {
        boolean a = false;
        double b = this.getAvgElv();
        int a1 = (int)Math.round(b);
        return (double)a1;
    }

    public MyLocation getLocation() {
        return this.currentLocation.clone();
    }

    public int getCurrentRSSI() {
        return this.currentRssi;
    }

    public CopyOnWriteArrayList<MyLocation> getLocations() {
        CopyOnWriteArrayList var1 = this.locationCache;
        synchronized(this.locationCache) {
            return (CopyOnWriteArrayList)this.locationCache.clone();
        }
    }

    public ArrayList getRSSI() {
        return (ArrayList)this.rssisCache.clone();
    }

    public boolean isLocated() {
        return this.located;
    }

    public void updateRSSI(int rssi) {
        this.rssisCache.add(Integer.valueOf(rssi));
        this.currentRssi = rssi;
        if(this.rssisCache.size() >= 7200) {
            this.rssisCache.remove(0);
        }

    }

    public void updateLocation(MyLocation loc) {
        if(loc == null) {
            LOG.V("TAGLocationDataManager", "--updateLocation--------------meiyou-chuanru--");
            this.located = false;
        } else {
            this.located = true;
            this.currentLocation = loc.clone();
            this.locationCache.add(loc.clone());
            if(this.locationCache.size() >= 7200) {
                this.locationCache.remove(0);
            }

            if(loc.getLatitude() > this.maxLat) {
                this.maxLat = loc.getLatitude();
            }

            if(loc.getLatitude() < this.minLat) {
                this.minLat = loc.getLatitude();
            }

            if(loc.getLongitude() > this.maxLon) {
                this.maxLon = loc.getLongitude();
            }

            if(loc.getLongitude() < this.minLon) {
                this.minLon = loc.getLongitude();
            }

            if(loc.getAltitude() > this.maxAlt) {
                this.maxAlt = loc.getAltitude();
            }

            if(loc.getAltitude() < this.minAlt) {
                this.minAlt = loc.getAltitude();
            }

            if((double)loc.getBaseDelay() > this.maxBaseDelay) {
                this.maxBaseDelay = (double)loc.getBaseDelay();
            }

            if((double)loc.getBaseDelay() < this.minBaseDelay) {
                this.minBaseDelay = (double)loc.getBaseDelay();
            }

            if((double)loc.getBaseLine() > this.maxBaseLine) {
                this.maxBaseLine = (double)loc.getBaseLine();
            }

            if((double)loc.getBaseLine() < this.minBaseLine) {
                this.minBaseLine = (double)loc.getBaseLine();
            }

        }
    }

    public void clearLocations() {
        this.locationCache.clear();
        this.rssisCache.clear();
        this.maxLat = 0.0D;
        this.minLat = 0.0D;
        this.maxLon = 0.0D;
        this.minLon = 0.0D;
        this.maxAlt = 0.0D;
        this.minAlt = 0.0D;
    }

    public double getMaxBaseDelay() {
        return this.maxBaseDelay;
    }

    public double getMinBaseDelay() {
        return this.minBaseDelay;
    }

    public double getMaxBaseLine() {
        return this.maxBaseLine;
    }

    public double getMinBaseLine() {
        return this.minBaseLine;
    }

    public double getMaxLatitude() {
        return this.maxLat;
    }

    public double getMinLatitude() {
        return this.minLat;
    }

    public double getMaxLongitude() {
        return this.maxLon;
    }

    public double getMinLongitude() {
        return this.minLon;
    }

    public double getMaxAltitude() {
        return this.maxAlt;
    }

    public double getMinAltitude() {
        return this.minAlt;
    }

    public CopyOnWriteArrayList<MySatellite> getSatellites() {
        return this.getSatellites(2, 1, 3);
    }

    public CopyOnWriteArrayList<MySatellite> getSatellites(int sys1, int sys2, int sys3) {
        return this.order(this.satellites, sys1, sys2, sys3);
    }

    public boolean isSatelliteFound() {
        return this.satelliteFound;
    }

    public void updateSatellites(CopyOnWriteArrayList<MySatellite> sats) {
        if(sats == null) {
            this.satelliteFound = false;
            LOG.V("TAGLocationDataManager", "-updateSatellites-----------meiyou-chuanru--");
        } else {
            this.satelliteFound = true;
            this.satellites = (CopyOnWriteArrayList)sats.clone();
            this.sort();
            this.bdsExist = false;
            this.gpsExist = false;
            this.glsExist = false;
            Iterator var3 = sats.iterator();

            while(var3.hasNext()) {
                MySatellite sat = (MySatellite)var3.next();
                switch(sat.getSystem()) {
                case 1:
                    this.gpsExist = true;
                    break;
                case 2:
                    this.bdsExist = true;
                    break;
                case 3:
                    this.glsExist = true;
                }
            }

        }
    }

    public String getGGA() {
        return this.strGGA;
    }

    protected void updateGGA(String gga) {
        if(gga != null) {
            this.strGGA = gga;
        }
    }

    private CopyOnWriteArrayList<MySatellite> order(CopyOnWriteArrayList<MySatellite> sat, int sys1, int sys2, int sys3) {
        CopyOnWriteArrayList result = new CopyOnWriteArrayList();
        int isys1 = 0;
        int isys2 = 0;
        int isys3 = 0;
        Iterator var10 = sat.iterator();

        while(var10.hasNext()) {
            MySatellite s = (MySatellite)var10.next();
            if(s.getSystem() == sys1) {
                result.add(isys1++, s);
                ++isys2;
                ++isys3;
            } else if(s.getSystem() == sys2) {
                result.add(isys2++, s);
                ++isys3;
            } else if(s.getSystem() == sys3) {
                result.add(isys3++, s);
            } else {
                result.add(s);
            }
        }

        return result;
    }

    private void sort() {
        Comparator comparator = new Comparator<MySatellite>() {
            public int compare(MySatellite lhs, MySatellite rhs) {
                return lhs.getPrn() - rhs.getPrn();
            }
        };
        String satePrn = "";

        MySatellite list;
        for(Iterator temp = this.satellites.iterator(); temp.hasNext(); satePrn = satePrn + "," + list.getPrn()) {
            list = (MySatellite)temp.next();
        }

        CopyOnWriteArrayList list1 = this.satellites;
        ArrayList temp1 = new ArrayList(list1);
        Collections.sort(temp1, comparator);
        this.satellites.clear();
        Iterator s2 = temp1.iterator();

        while(s2.hasNext()) {
            MySatellite satePrn2 = (MySatellite)s2.next();
            this.satellites.add(satePrn2);
        }

        String satePrn21 = "";

        MySatellite s21;
        for(Iterator var7 = this.satellites.iterator(); var7.hasNext(); satePrn21 = satePrn21 + "," + s21.getPrn()) {
            s21 = (MySatellite)var7.next();
        }

    }

    private double getAvgElv() {
        if(this.satellites.size() < 6) {
            LOG.V("TAGLocationDataManager", "--------satellites.size() < 6---------------");
            return 0.0D;
        } else {
            double[] temp = new double[60];
            double[] tempSnr = new double[60];
            int countSat = 0;

            for(Iterator j = this.satellites.iterator(); j.hasNext(); ++countSat) {
                MySatellite avg = (MySatellite)j.next();
                temp[countSat] = avg.getElevation();
                tempSnr[countSat] = avg.getSnr();
            }

            for(int var10 = 0; var10 < countSat; ++var10) {
                for(int var12 = var10; var12 < countSat; ++var12) {
                    if(temp[var10] < temp[var12]) {
                        double i = temp[var10];
                        temp[var10] = temp[var12];
                        temp[var12] = i;
                        double tSnr = tempSnr[var10];
                        tempSnr[var10] = tempSnr[var12];
                        tempSnr[var12] = tSnr;
                    }
                }
            }

            double var11 = 0.0D;

            for(int var13 = 0; var13 < 6; ++var13) {
                var11 += tempSnr[var13];
            }

            var11 /= 6.0D;
            LOG.V("TAGLocationDataManager", " ----avg-avg-----ELV------------" + var11);
            return var11;
        }
    }

    private double getAvgSnr() {
        if(this.satellites.size() < 6) {
            return 0.0D;
        } else {
            double[] temp = new double[60];
            int countSat = 0;

            for(Iterator j = this.satellites.iterator(); j.hasNext(); ++countSat) {
                MySatellite avg = (MySatellite)j.next();
                temp[countSat] = avg.getSnr();
            }

            for(int var7 = 0; var7 < countSat; ++var7) {
                for(int var9 = var7; var9 < countSat; ++var9) {
                    if(temp[var7] < temp[var9]) {
                        double i = temp[var7];
                        temp[var7] = temp[var9];
                        temp[var9] = i;
                    }
                }
            }

            double var8 = 0.0D;

            for(int var10 = 0; var10 < 6; ++var10) {
                var8 += temp[var10];
            }

            var8 /= 6.0D;
            LOG.V("TAGLocationDataManager", " ----avg-avg------SNR-----------" + var8);
            return var8;
        }
    }

    public long getSDFreeSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        long blockSize = (long)sf.getBlockSize();
        long freeBlocks = (long)sf.getAvailableBlocks();
        return freeBlocks * blockSize / 1024L / 1024L;
    }
}
